package com.amsure.service.impl;

import com.amsure.entity.BaseCommonEntity;
import com.amsure.service.AccountChargeService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

import static com.amsure.entity.ConstantEntity.POSTINFORMATIONFORDATA;
import static com.amsure.entity.UrlEntity.URL_ACCOUNT_CHARGE;
import static com.amsure.utils.CommonUtils.getCorrespondingValueFromInformation;
import static com.amsure.utils.CommonUtils.getTradeNo;
import static com.amsure.utils.EncodeUtils.*;
import static com.amsure.utils.HttpUtils.DO_POST;

/**
 * @date 2021/8/22
 * @ddescription:
 */
@Service
public class AccountChargeServiceImpl implements AccountChargeService {
    /**
     * @Description: 账户充值，发送服务端
     * @Param:
     * @return:
     */
    @Override
    public HashMap<String, Object> sendElectricChargePost(BaseCommonEntity baseAccountChargeEntity) throws Exception {
        //实体类中set订单号
        baseAccountChargeEntity.setTradeNo(getTradeNo(baseAccountChargeEntity.getOperatorId(), baseAccountChargeEntity.getTimeStamp()));
        //加密data
        BaseCommonEntity baseEncodeDataForAccountChargeCommonEntity = encodeDataForAccountCharge(baseAccountChargeEntity);
        //转换充值信息，注意，这边不是扣费的
        String stringReadyPostForToken = sendPostMaptoString(baseEncodeDataForAccountChargeCommonEntity);
        //post
        HashMap<String, String> stringStringHeaderHashMap = new HashMap<>();
        String accessToken = baseEncodeDataForAccountChargeCommonEntity.getAccessToken();
        stringStringHeaderHashMap.put("authorization", accessToken);
        String stringReceivePostForToken = DO_POST(URL_ACCOUNT_CHARGE, stringReadyPostForToken, stringStringHeaderHashMap, null);
        //获取data
        String stringReceivePostForData = getPartFromAll(stringReceivePostForToken, POSTINFORMATIONFORDATA);
        //解密data
        String stringDecodeData = decodeInformation(stringReceivePostForData, baseEncodeDataForAccountChargeCommonEntity);
        //返回结果
        return getCorrespondingValueFromInformation(stringReceivePostForToken,stringDecodeData);
        }

}

